import requests
import json
import csv
from pyquery import PyQuery as jq
import pandas as pd
import logging
from termcolor import colored
import re
logging.basicConfig(
format='[%(asctime)s]%(message)s', level=logging.INFO)
Loger = logging.getLogger('zhihu')

def success(txt): return Loger.info(f"{colored(txt, 'green')}")
def error(txt): return Loger.info(f"{colored(txt, 'red')}")
def info(txt): return Loger.info(f"{colored(txt, 'blue')}")
def warning(txt): return Loger.info(f"{colored(txt, 'yellow')}")
data = ""
headers = json.loads(r'''{
	"Accept": "application/json, text/javascript, */*; q=0.01",
	"Accept-Encoding": "gzip, deflate, br",
	"Accept-Language": "zh-CN,zh;q=0.9",
	"Connection": "keep-alive",
	"Host": "m.maoyan.com",
	"Referer": "https://m.maoyan.com/search?searchtype=movie&$from=canary",
	"User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Mobile Safari/537.36",
	"X-Requested-With": "XMLHttpRequest"
}''')
datacol = {0:'剧名',1:'评分',2:'评分人数'}
class maoyan():
	def __init__(self):
		self.keylist = []
		self.alllist = []
		self.searchurl = "https://m.maoyan.com/ajax/search?cityId=50&stype=-1&kw="
	def getPingfen(self,id,key):
		pfurl = f"https://m.maoyan.com/movie/{id}?_v_=yes&channelId=4&cityId=50&$from=canary"
		resp = requests.get(pfurl, data=data, headers=headers)
		alldiv = jq(resp.text)('#app > div > div.movie-page > section.movie-header > div > div.movie-container.clearfix > div.movie-content > div.movie-score > div')
		rating = alldiv('.rating').text()
		num = alldiv('div.score-num').text()
		if num:
			num = re.findall("\d+",num)[0]
		datas = [key,rating,num]
		success(f'[{key}] {rating}: {num}')
		self.alllist.append(datas)
	def search(self,key):
		resp = requests.get(self.searchurl+key, data=data, headers=headers)
		myjson = json.loads(resp.text)
		if 'movies' in myjson:
			for i in myjson['movies']['list']:
				print(f"nm:{i['nm']} :::: {key}")
				if i['nm'] == key:
					self.getPingfen(i['id'],i['nm'])
		else:
			warning(key)
	def start(self):
		try:
			with open('剧单.csv',encoding='utf8') as f:
				print(len(list(csv.reader(f))))
				# for line in csv.reader(f):
				# 	self.search(line[1])
		finally:
			with open('maoyan.json', 'w', encoding='utf-8') as f:
				f.write(json.dumps(self.alllist,ensure_ascii=False))
			df = pd.DataFrame(self.alllist)
			df.rename(columns=datacol,inplace=True)
			df.to_excel('maoyan_pf.xlsx')






if __name__ == '__main__':
	maoyan().start()
